package com.fourGirlsAndADude.mobileDRT;

import java.util.ArrayList;

import com.att.intern.webservice.Incident;
import com.att.intern.webservice.Webservice;
import com.att.intern.webservice.Webservice.TokenInvalidException;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class AdminConditionCannedReportActivity extends Activity {
	// UI elements
	private TextView header;
	private TextView subHeader;
	private ListView listView;

	// Used to hold extras retrieved from intent
	private String token;
	private String eventName;
	private int year;

	// Used to display List
	private ArrayAdapter<String> displayList;
	private ArrayList<Incident> fromWebService = new ArrayList<Incident>();
	private ArrayList<String> buildingNamesToBeDisplayed = new ArrayList<String>();
	private String[] buildingInfoToBeDisplayed = new String [12];

	private String event = "Event: ";
	private String eventYear = "Event Year: ";
	private String glc = "GLC: ";
	private String building;
	private String address = "Address: ";
	private String state = "State: ";
	private String type = "Type: ";
	private String assessment = "Assessment: ";
	private String status = "Status: ";
	private String damaged = "Damaged: ";
	private String commericalPower = "Commercial Power: ";
	private String onGenerator = "On Generator: ";
	private String unOccupiable = "Unoccupiable: ";

	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin_condition_canned_report);
		token = getIntent().getStringExtra("SESSION_TOKEN");
		Log.v("TOKEN CHECK 3", "Token: "+ token);
		eventName = getIntent().getStringExtra("EVENT_NAME");
		year = getIntent().getIntExtra("EVENT_YEAR", 0);
		Log.v("ACCR", "values in");
		intializeArrayList(token, eventName, year);
		
		//Display TextView
		header = (TextView) findViewById(R.id.header);
		subHeader = (TextView) findViewById(R.id.damagerptresults);
		listView = (ListView) findViewById(R.id.listView1);
		
		//Create ArrayAdapter
		getBuildingNamesForDisplay();
		displayList = new ArrayAdapter<String>(AdminConditionCannedReportActivity.this,
				android.R.layout.simple_list_item_1, buildingNamesToBeDisplayed);	
		
		//Add to list view
		listView.setAdapter(displayList);
		listView.setOnItemClickListener(new OnListViewClicked());
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.admin_condition_canner_report, menu);
		return true;
	}
	
	private class OnListViewClicked implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Log.v("ACCR", "In on clicked");
			building = (String)arg0.getItemAtPosition(arg2);
			getBuildingInfoForDisplay(fromWebService.get(arg2));
			// TODO Auto-generated method stub
			AlertDialog.Builder Info = new AlertDialog.Builder(AdminConditionCannedReportActivity.this);
			Info.setTitle(building)
			.setItems(buildingInfoToBeDisplayed, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			}).setNegativeButton(R.string.Exit, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.cancel();
				}
			});
			Log.v("ACCR", "Created Info");
			AlertDialog showInfo = Info.create();
			Log.v("ACCR", "Displaying Info");
			showInfo.show();
		}
		
	}

	private void intializeArrayList(String token, String eventName,
			int eventYear) {
		try {
			
			Log.v("ACCR", "Test 1");
			fromWebService.size();
			Log.v("ACCR", "Test 2");
			fromWebService = LoginActivity.webService
					.getAdminConditionCannedReport(token, eventName, eventYear);
			Log.v("ACCR", "Test 3");
			for (Incident i : fromWebService) {
				Log.v("ACCR","Building Name: " + i.getBuildingName());
			}
			
			Log.v("ACCR", "Building Names Displayed");
		} catch (TokenInvalidException e) {
			Intent i = new Intent(getBaseContext(), LoginActivity.class);
			startActivity(i);
		} 

	}
	
	private void getBuildingNamesForDisplay(){
		for(int i = 0; i<fromWebService.size(); i++){
			buildingNamesToBeDisplayed.add(fromWebService.get(i).getBuildingName());
		}
	}
	
	private void getBuildingInfoForDisplay(Incident incident){
		event += incident.getEventName();
		eventYear += eventYear;
		glc += incident.getGeoLoc();
		address += incident.getBuildingAddress();
		state += incident.getState();
		type += incident.getBuildingType();
		assessment += incident.getAssessNotes();
		status += incident.getStatusNotes();
		damaged += incident.getDamageIndicator();
		commericalPower += incident.getComPowerIndicator();
		onGenerator += incident.getOnGeneratorIndicator();
		unOccupiable += incident.getUnOccupiableIndicator();
		
		//Add to string array
		buildingInfoToBeDisplayed[0] = event;
		buildingInfoToBeDisplayed[1] = eventYear;
		buildingInfoToBeDisplayed[2] = glc;
		buildingInfoToBeDisplayed[3] = address;
		buildingInfoToBeDisplayed[4] = state;
		buildingInfoToBeDisplayed[5] = type;
		buildingInfoToBeDisplayed[6] = assessment;
		buildingInfoToBeDisplayed[7] = status;
		buildingInfoToBeDisplayed[8] = damaged;
		buildingInfoToBeDisplayed[9] = commericalPower;
		buildingInfoToBeDisplayed[10] = onGenerator;
		buildingInfoToBeDisplayed[11] = unOccupiable;
		
	}
	

}
